﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ChoDiaOc_HK_WPF.Controls;
using ChoDiaOc_HK_WPF.Models;

namespace ChoDiaOc_HK_WPF.Views.GiaHanDichVu {
    /// <summary>
    /// Interaction logic for GiaHanDichVu.xaml
    /// </summary>
    public partial class HuyDichVu : UserControl {

        private KHACHHANG khachHangSource;

        public KHACHHANG KhachHangSource {
            get { return khachHangSource; }
            set { khachHangSource = value; }
        }

        private PHIEUDANGKY phieuDangKySource;

        public PHIEUDANGKY PhieuDangKySource {
            get { return phieuDangKySource; }
            set { phieuDangKySource = value; }
        }

        private DIAOC diaOcSource;

        public DIAOC DiaOcSource {
            get { return diaOcSource; }
            set { diaOcSource = value; }
        }

        private LOAIDIAOC loaiDiaOcSource;

        public ChoDiaOc_HK_WPF.LOAIDIAOC LoaiDiaOcSource {
            get { return loaiDiaOcSource; }
            set { loaiDiaOcSource = value; }
        }
        public HuyDichVu() {
            InitializeComponent();

            phieuDangKySource = (PHIEUDANGKY)FindResource("phieuDangKySource");
            khachHangSource = (KHACHHANG)FindResource("khachHangSource");
            diaOcSource = (DIAOC)FindResource("diaOcSource");
            loaiDiaOcSource = (LOAIDIAOC)FindResource("loaiDiaOcSource");


            HuyDangKyController.GetInstance().PhieuDangKy = phieuDangKySource;
            HuyDangKyController.GetInstance().KhachHang = khachHangSource;
            HuyDangKyController.GetInstance().DiaOc = diaOcSource;
            HuyDangKyController.GetInstance().LoaiDiaOc = loaiDiaOcSource;

            tuNgayTextBlock.Visibility = Visibility.Hidden;
            denNgayTextBlock.Visibility = Visibility.Hidden;
            dienTichDatTextBlock.Visibility = Visibility.Hidden;
            dienTichXayDungTextBlock.Visibility = Visibility.Hidden;

        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e) {
        }

        private bool isValidate = false;

        private void btnNhapMa_Click(object sender, RoutedEventArgs e) {

            PHIEUDANGKY phieu = DatabaseWrapper.GetInstance().GetPhieuDangKy(phieuDangKySource.MaPhieuDangKy);
            isValidate = false;
            if (phieu == null) {
                MessageBox.Show("Không tìm thấy phiếu đăng ký!", "Cảnh báo", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.Cancel, MessageBoxOptions.None);
                isValidate = false;
                return;
            } else {
                if (phieu.DIAOC.TrangThai == "Đã bán") {
                    MessageBox.Show("Đăng ký này đã bị huỷ!", "Cảnh báo", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.Cancel, MessageBoxOptions.None);
                    isValidate = false;
                    return;
                }
                isValidate = true;
                tuNgayTextBlock.Visibility = Visibility.Visible;
                denNgayTextBlock.Visibility = Visibility.Visible;
                dienTichDatTextBlock.Visibility = Visibility.Visible;
                dienTichXayDungTextBlock.Visibility = Visibility.Visible;

                phieuDangKySource.TuNgay = phieu.TuNgay;
                phieuDangKySource.DenNgay = phieu.DenNgay;
                phieuDangKySource.SoTien = phieu.SoTien;

                khachHangSource.HoTen = phieu.KHACHHANG.HoTen;
                khachHangSource.DiaChi = phieu.KHACHHANG.DiaChi;
                khachHangSource.SoDT = phieu.KHACHHANG.SoDT;
                khachHangSource.Email = phieu.KHACHHANG.Email;

                diaOcSource.MaDiaOc = phieu.DIAOC.MaDiaOc;
                diaOcSource.DiaChi = phieu.DIAOC.DiaChi;
                diaOcSource.DienTichDat = phieu.DIAOC.DienTichDat;
                diaOcSource.DienTichXayDung = phieu.DIAOC.DienTichXayDung;
                diaOcSource.Huong = phieu.DIAOC.Huong;
                diaOcSource.ViTri = phieu.DIAOC.ViTri;
                diaOcSource.MoTa = phieu.DIAOC.MoTa;
                diaOcSource.TrangThai = phieu.DIAOC.TrangThai;
                diaOcSource.GiaBan = phieu.DIAOC.GiaBan;
                loaiDiaOcSource.TenLoaiDiaOc = phieu.DIAOC.LOAIDIAOC.TenLoaiDiaOc;
                loaiDiaOcSource.KyHieu = phieu.DIAOC.LOAIDIAOC.KyHieu;
            }

        }

        private void btnHuy_Click(object sender, RoutedEventArgs e) {
            if (isValidate) {
                tuNgayTextBlock.Visibility = Visibility.Hidden;
                denNgayTextBlock.Visibility = Visibility.Hidden;
                dienTichDatTextBlock.Visibility = Visibility.Hidden;
                dienTichXayDungTextBlock.Visibility = Visibility.Hidden;
                isValidate = false;              

                if (MessageBoxResult.OK == MessageBox.Show("Xác nhận huỷ đăng ký?", "Cảnh báo", MessageBoxButton.OKCancel, MessageBoxImage.Warning, MessageBoxResult.Cancel, MessageBoxOptions.None)) {
                    if (HuyDangKyController.GetInstance().Huy()) {
                        MessageBox.Show("Huỷ thành công!", "Cảnh báo", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.Cancel, MessageBoxOptions.None);
                    }
                }

            }
        }

    }
}
